package com.foxconn.ipebg.common.base.shcedule;



import ipebg.mzk.common.util.PropertyPhase;
import ipebg.mzk.facade.entity.LogMongo;
import ipebg.mzk.facade.enums.LogLevel;
import ipebg.mzk.facade.system.service.LogService;
import ipebg.mzk.facade.system.service.MsAccessCountRecordService;

import javax.annotation.Resource;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class PersistentWebAccessLogJob {
	private static final Log log = LogFactory.getLog(PersistentWebAccessLogJob.class);
	@Resource(name = "msAccessCountRecordServiceImpl")
	MsAccessCountRecordService msAccessCountRecordServiceImpl;

	@Resource(name = "logServiceImpl")
	private LogService logServiceImpl;

	public void execute() {
		try {
			// 只需一台AP执行
			log.info("PersistentWebAccessLog start.");
			logServiceImpl.save(new LogMongo(LogLevel.INFO, "Prepare to persistent redis data in database."));

			String SCHEDULE_RUN_STATE = PropertyPhase.readData("SCHEDULE_RUN_STATE");
			if (SCHEDULE_RUN_STATE.equals("FALSE"))
				return;
			log.info("PersistentWebAccessLog running.");
			msAccessCountRecordServiceImpl.Persistent2DB();
			logServiceImpl.save(new LogMongo(LogLevel.INFO, "Persistent web access log from redis in database complete."));
		} catch (Exception e) {
			log.error(e.getMessage());
		}
	}
}
